<?php
/**
 * 会员前台管理中心、账号管理、收藏操作类
 */

defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('foreground');
pc_base::load_sys_class('format', '', 0);
pc_base::load_sys_class('form', '', 0);

class designer extends foreground {

	private $works_db;
	private $designer_db;
	private $certificate_db;
	function __construct() {
		parent::__construct();
		$this->http_user_agent = str_replace('7.0' ,'8.0',$_SERVER['HTTP_USER_AGENT']);
		$this->designer_db = pc_base::load_model('designer_model');
		$this->works_db = pc_base::load_model('works_model');
		$this->certificate_db = pc_base::load_model('certificate_model');
		
	}

	public function init() {
		$memberinfo = $this->memberinfo;
		//初始化phpsso
		$phpsso_api_url = $this->_init_phpsso();
		//获取头像数组
		$avatar = $this->client->ps_getavatar($this->memberinfo['phpssouid']);

		$grouplist = getcache('grouplist');
		print_r($grouplist);
		$memberinfo['groupname'] = $grouplist[$memberinfo[groupid]]['name'];

		include template('member', 'index');
	}
	/**
	 * 
	 * 增加作品
	 */
	function works(){
		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['work'];
			$data['userid']= $this->memberinfo['userid'];
			$this->works_db->add_works($data);
			showmessage(L('operation_success'), HTTP_REFERER);
		}
		else
		{
			$units = getcache('3366', 'linkage');
			$unit = $units['data'];
			foreach ($unit as $k=>$v){
				if($v['parentid']!=0){
					unset($unit[$k]);
				}
			}
			$styles = getcache('3370', 'linkage');
			$style = $styles['data'];
		
			include template('member', 'add_work');
		}
		
	}
	/*处理设计师信息*/
	function design_info(){
		$member = $this->designer_db->get_one(array('userid'=>$this->memberinfo['userid']));

		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['designer'];
			if(is_array($member)){
				$this->designer_db->update($data, array('userid'=>$member['userid']));
			}
			else
			{
				$data['userid']= $this->memberinfo['userid'];
				$this->designer_db->insert($data);
			}
			showmessage(L('operation_success'), HTTP_REFERER);
		}
		else
		{
			include template('member', 'design_info');
		}
	}
	/**
	 * 
	 * ajax取得空间类型
	 */
	function get_space_type(){
		$types = getcache('3366', 'linkage');
		$data = $types['data'];
		$id = $_POST['id'];
		$str = '<select name="work[space_type2]">';
		if($id!=0){
			if(is_array($data))
			{
				foreach ($data as $k=>$v){
				
					if($v['parentid']!= $id || $v['parentid']==0){
						unset($data[$k]);
					}
				}
				
				foreach ($data as $key=>$val){
					$str .= '<option value="'.$val['linkageid'].'">'.$val['name'].'</option>';
				}
				
				$str .= '</select>';
			}	
		}
		else
		{
			$str .= '<option vaule="">请选择</option>';	
		}
		$str .= '</select>';
		$data = array();
		echo $str;
	}
	/**
	 * 
	 * 证书管理
	 */
	function certificate(){
		if(isset($_POST['dosubmit']))
		{
			$data = $_POST['info'];
			$data['userid']= $this->memberinfo['userid'];
			$this->certificate_db->insert($data);
			showmessage(L('operation_success'), HTTP_REFERER);
			
		}
		include template('member', 'desginer_cert');
	}
	function list_certificate(){
		include template('member', 'designer_certificate');
	}
function message(){

		$this->messages_db = pc_base::load_model('messages_model');
		
		$table_pre = $this->messages_db->db_tablepre;
		$member_company_db = $table_pre.'member_company';
		$message_db = $table_pre.'messages';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 15;
		$offset = intval($pagesize*($page-1));
		
		$sql = '
		SELECT * FROM `v9_messages` WHERE rid=0 and `replyid`=0 and userid ='.$this->memberinfo['userid'];
		

			//$sql = 'select a.* from '.$message_db.' as a left join 
			//'.$member_company_db.' as b on a.companyid=b.companyid where a.companyid = //'.intval($_GET['companyid']);
			$sql_count = 'SELECT count(*) as num FROM `v9_messages` WHERE rid=0 and `replyid`=0 and userid ='.$this->memberinfo['userid'];
		
		$sql .= " LIMIT $offset,$pagesize";
		//echo $sql;exit;
		$this->messages_db->query($sql);
		$message_data = $this->messages_db->fetch_array();
		$this->messages_db->query($sql_count);
		$total = $this->messages_db->fetch_array();
		
		$total = $total[0]['num'];
		
		$pages = pages($total, $page, $pagesize);
		//;
		$data = $this->company_data;
		include template('member', 'designer_message');
	}
/**
	 * 
	 * 业主发起对公司的签约列表
	 */
	function signing(){
		$memberinfo = $this->memberinfo;
		$userid = $memberinfo['userid'];
	
		$this->signed_db = pc_base::load_model('signed_model');
		$table_pre = $this->signed_db->db_tablepre;
		$member_designer_db = $table_pre.'member_designer';
		$signed_db = $table_pre.'signed';
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 15;
		$offset = intval($pagesize*($page-1));
		$signedid = intval($_GET['signedid']);
		if($signedid){$con = ' and signedid='.$signedid;}
			$sql = 'select a.*,b.name from '.$signed_db.' as a left join 
			'.$member_designer_db.' as b on a.c_userid =b.userid  where a.c_userid = '.$userid.$con;
			$sql_count = 'select count(a.signedid) as num from '.$signed_db.' as a left join 
			'.$member_designer_db.' as b on a.c_userid=b.userid where a.c_userid = '.$userid.$con;
		
		$sql .= " LIMIT $offset,$pagesize";
		
		$this->signed_db->query($sql);
		$signed_data = $this->signed_db->fetch_array();
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		
		$total = $total[0]['num'];
		
		$pages = pages($total, $page, $pagesize);
		include template('member', 'designer_signed');
	}
	//公司删除签约
	function del_signed(){
		$signedid = $_GET['signedid'];
		if(intval($signedid)<1) return ;
		$this->signed_db = pc_base::load_model('signed_model');
		$s = $this->signed_db->delete('signedid='.$signedid);
		$msg =  $s>0 ? '删除成功':'删除失败'; 
		showmessage($msg, HTTP_REFERER);	
	}
	
	///确定和业主签约
	function sure_signed(){
		$signedid = intval($_GET['signedid']);
		if($signedid<1) return '';
		$this->signed_db = pc_base::load_model('signed_model');
		$result = $this->signed_db->update('is_sure=1', 'signedid='.$signedid);
		$msg = $result==1?'同意签约成功，你可以给该业主发感谢站内信':'签约失败';
		showmessage($msg, HTTP_REFERER);
	}
	//业主对公司的评价
	function appraisal(){
		$this->signed_db = pc_base::load_model('signed_model');
		if($_POST['dosubmit']){
			$data = $_POST['rcontent'];
			$signedid = $_POST['signedid'];
			$result = $this->signed_db->update('rcontent=\''.$data.'\'', 'signedid='.$signedid);
			$msg = $result==1?'回复成功':'回复失败';
			showmessage($msg, HTTP_REFERER);
		}else{
			$signedid = intval($_GET['signedid']);
			if($signedid<1) return '';
			$signed_data = $this->signed_db->get_one('signedid='.$signedid);
			include template('member', 'designer_appraisal');
		}
	}
	//评价列表
	function appraisal_list(){
		$this->signed_db = pc_base::load_model('signed_model');
		$page = $_GET['page']?$_GET['page']:1 ;
		$pagesize = 10;
		$offset = intval($pagesize*($page-1));
	
		$sql = 'SELECT *  FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->memberinfo['userid'].' order by signedid desc ';

		$sql_count = 'select count(signedid) as num FROM '.$this->signed_db->table_name.' WHERE c_userid='.$this->memberinfo['userid'].' order by signedid desc ';
		
		$sql .= " LIMIT $offset,$pagesize ";
		
		$this->signed_db->query($sql);
		$pj_data = $this->signed_db->fetch_array();
		//print_r($pj_data);exit;
		$this->signed_db->query($sql_count);
		$total = $this->signed_db->fetch_array();
		$total = $total[0]['num'];
		$pagesbar = pages($total, $page, $pagesize);
		include template('member', 'designer_appraisal_list');
	}
}
?>